package mylab.io.core;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import mylab.base.foundation.lang.Callback;
import mylab.io.base.model.Msg;
import mylab.io.base.property.DasProperties;
import mylab.io.core.codec.Codec;
import mylab.io.core.context.CoreContext;
import mylab.io.core.util.NettyUtil;

/**
 * 自实现接入服务模板类
 * <>
 * client模式：一般只有一个 EventLoopGroup， 即一个Reactor线程池
 * </>
 */
public abstract class AbstractClientDas<P extends DasProperties, M extends Msg, C extends Codec<P, M, F>, F>
        extends AbstractDas<P, M, C, F> {

    protected EventLoopGroup masterLoop;
    protected ChannelFuture clientFuture;
    protected Bootstrap bootstrap;

    public AbstractClientDas(CoreContext<P, M> context, C codec) {
        super(context, codec);
    }

    @Override
    protected void stopFunc(Callback<Void> callback) {
        try {
            NettyUtil.closeChannel(channel);
        } catch (Exception e) {
            e.printStackTrace(System.out);
        } finally {
            if (masterLoop != null) {
                masterLoop.shutdownGracefully();
            }
            channel = null;
        }

        Callback.dealOnSuccess(callback);
    }
}
